﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CEED.Utils;
using System.IO;

namespace CEED.Utils
{
    class Xorer
    {
        public static void TramsformBlock(byte[] data, byte[] xorpad)
        {
            for (var i = 0; i < data.Length; i++)
                data[i] = (byte)(xorpad[i] ^ data[i]);
        }
        public static void TramsfoemStream(Stream dstfs, Stream srcfs, Stream xorfs,long offset, long length)
        {
            xorfs.Seek(offset, SeekOrigin.Current);
            srcfs.Seek(offset, SeekOrigin.Current);

            long buffersize = 1024 * 1024 * 4;
            byte[] buffer;
            byte[] xorbuffer;
            buffer = new byte[buffersize];
            xorbuffer = new byte[buffersize];
            while (length > 0)
            {
                buffersize = length > buffersize ? buffersize : length;
                srcfs.Read(buffer, 0, (int)buffersize);
                xorfs.Read(xorbuffer, 0, (int)buffersize);
                TramsformBlock(buffer, xorbuffer);
                dstfs.Write(buffer, 0, (int)buffersize);
                length -= buffersize;
            }
        }
    }
}
